# generic modules
import cherrypy
import math
import uuid
# fressen modules
import secMod
import mlt
import html
#class ReturnRange:
#    def index(self, first=None, last=None):
#        lijst = []

#        return "%s" % (hmtl.header, html.menu, str(lijst), html.footer)
#    index.exposed = True

class MLT:
    def index(self, cohort=100000, femratio=50, sex='Male', tabletype ='CD East',  E0='20', function='standalone'):
        pagetitle = "Model Life Table"
        functions = ['pyramid','standalone']
        def error(value): 
            message = "%s %s %s %s %s %s %s" % (html.header(pagetitle), html.menu, "<em>", str(value) , "</em>" ,"not a valid value", html.footer)
            return message
        # Programming defensively (user input)
        if secMod.numcheck(cohort, "integer"):
            if secMod.numcheck(femratio, "integer") and int(femratio) >= 1 and int(femratio) <= 100:
                if sex in ['Male','Female']:
                    if tabletype in mlt.tableType():   
                        if secMod.numcheck(E0, "integer") and E0 in mlt.E0values():
                            if secMod.stringcheck(function, functions):
                                if function == 'standalone':
                                    uniqid = str(uuid.uuid4())
                            # to prevent sql injection
                                    t = (tabletype, sex, E0,)
                                    tabledescription = tabletype + " " + sex + ", E0: " + E0 + " & cohort: " + str(cohort) + ", ratio females : %" + str(int(femratio))  
                                    queryresult = mlt.getPyramid(t, cohort, int(femratio))
                                    return "%s %s %s %s %s %s %s %s" % (html.header(pagetitle), html.menu, html.pyramid(queryresult['categories'], tabledescription, cohort, queryresult['femdata'],queryresult['maledata'], uniqid), html.pyramidcontainer(uniqid), html.pyramidtable(queryresult['categories'], queryresult['femdata'],queryresult['maledata']), html.footer, html.standalone(mlt.tableType(), tabletype, mlt.E0values(), E0, sex, femratio, cohort), mlt.calculateC(t)) 
                                elif function == 'pyramid':
                                    uniqid = str(uuid.uuid4())
                            # to     prevent sql injection
                                    t = (tabletype, sex, E0,)
                                    tabledescription = tabletype + " " + sex + ", E0: " + E0 + " & cohort: " + str(cohort) + ", ratio females : %" + str(int(femratio))  
                                    queryresult = mlt.getPyramid(t, cohort, int(femratio))
                                    return "%s %s %s" % (html.pyramid(queryresult['categories'], tabledescription, cohort, queryresult['femdata'],queryresult['maledata'], uniqid), html.pyramidcontainer(uniqid), html.pyramidtable(queryresult['categories'], queryresult['femdata'],queryresult['maledata'])) 

                            else:
                                return error('function')
                        else:
                            return error('E0') 
                    else:
                        return error('tabletype')
                else:
                    return error('sex') 
            else:
                return error('femratio') 
        else:
            return error('cohort') 
    index.exposed = True

class Fressen:
#    lijstje = ReturnRange()
    mlt = MLT()
    def index(self):
        pagetitle = "Erst kommt das Fressen&hellip;"
        return "%s %s %s %s" % (html.header(pagetitle), html.menu, html.maintext(), html.footer)
    index.exposed = True
root = Fressen()
# --
if __name__ == '__main__': import os.path
current_dir = os.path.dirname(os.path.abspath(__file__))
### prod settings: cherrypy.config.update({'environment': 'production', 'log.screen': True})

conf = {'global':       {'server.socket_port': 8880,
                        'server.socket_host': '0.0.0.0'},
        '/':            {'tools.staticdir.root': current_dir},
        '/static/css':  {'tools.gzip.on': True,
                        'tools.gzip.mime_types':['text/css'],
                        'tools.staticdir.on': True,
                        'tools.staticdir.dir': 'static/css'},
        '/static/js':   {'tools.gzip.on': True,
                        'tools.gzip.mime_types':['application/javascript'],
                        'tools.staticdir.on': True,
                        'tools.staticdir.dir': 'static/js'},
        '/static/images': {'tools.staticdir.on': True,
                            'tools.staticdir.dir': 'static/images'}
}

cherrypy.quickstart(Fressen(), '/', config=conf)
# --
